﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;

namespace WebService_Newstore.DAO
{
    public class CommandeDAO:BaseDAO
    {
        public int idCommande { get; set; }
        public DateTime dateCmd { get; set; }
        public DateTime dateLivrCmd { get; set; }
        public string refCommande { get; set; }
        public int idHyper { get; set; }          //reference de la commande
        public int statutCmd { get; set; }
        public int idUser { get; set; }                 //utilisateur qui a pris la commande


        public CommandeDAO() { }

        public CommandeDAO(int idCommande, DateTime dateCmd, DateTime dateLivrCmd, string refCommande, int idHyper, int statutCmd, int idUser)
        {
            this.idCommande = idCommande;
            this.dateCmd = dateCmd;
            this.dateLivrCmd = dateLivrCmd;
            this.refCommande = refCommande;
            this.idHyper = idHyper;
            this.statutCmd = statutCmd;
            this.idUser = idUser;
        }

        public static List<CommandeDAO> getCommandeDAOByDate(DateTime dateJour)
        {
            List<CommandeDAO> listeCmd = new List<CommandeDAO>();

            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var query = from commande in newStoreContext.commandes
                                where (commande.dt_liv_cmd <= dateJour && commande.statut_cmd <=1)
                                select commande;

                    if (query.LongCount() == 0)
                        throw new Exception("Aucune commande ne correspond !!");

                    foreach (var cmd in query)
                    {
                        Console.WriteLine("idCommande => " + cmd.id_cmd);

                        //On instancie la commande DAO
                        CommandeDAO commande = new CommandeDAO();
                        commande.idCommande = cmd.id_cmd;
                        commande.dateCmd = (DateTime)cmd.dt_cmd;
                        commande.dateLivrCmd = (DateTime)cmd.dt_liv_cmd;
                        commande.refCommande = cmd.ref_cmd;
                        commande.statutCmd = (int)cmd.statut_cmd;
                        commande.idHyper = (int)cmd.id_hyper;
                        commande.idUser = (int)cmd.id_util;
                        //On l'ajoute à la liste des commandes du jour
                        listeCmd.Add(commande);
                    }

                    return listeCmd;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    return listeCmd;
                }

            }
        }

        public static void UpdateCmd(CommandeDAO cmd)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    commande c = newStoreContext.commandes.FirstOrDefault(i => i.id_cmd == cmd.idCommande);
                    if (c != null)
                    {
                        c.dt_cmd = cmd.dateCmd;
                        c.dt_liv_cmd = cmd.dateLivrCmd;
                        c.ref_cmd = cmd.refCommande;
                        c.id_hyper = cmd.idHyper;
                        c.id_util = cmd.idUser;
                        c.statut_cmd = cmd.statutCmd;
                        newStoreContext.SaveChanges();
                    }
                    else
                    {
                        Console.WriteLine("Pas d'enregistrement à modifier !!");
                        //Ici on mettrait le code pour l'insertion
                    }
                    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.InnerException);
                }
            }
        }

        public static CommandeDAO GetCmdById(int idCmd)
        {
            CommandeDAO cmdRetour = new CommandeDAO();

            using (newStoreContext = new newStoreEntities())
            {
                try
                {

                    var userQuery = from commande in newStoreContext.commandes
                                    where commande.id_cmd == idCmd
                                    select commande;

                    if (userQuery.LongCount() == 0)
                    {
                        //On instancie un DAO vide pour faire remonter l'erreur au niveau du controller
                        cmdRetour.idCommande = 0;
                    }

                    foreach (var ligne in userQuery)
                    {
                        //On instancie la ligne de BP
                        cmdRetour.idCommande = ligne.id_cmd;
                        cmdRetour.idHyper = (int)ligne.id_hyper;
                        cmdRetour.idUser = (int)ligne.id_util;
                        cmdRetour.refCommande = ligne.ref_cmd;
                        cmdRetour.statutCmd = (int)ligne.statut_cmd;
                        cmdRetour.dateCmd = (DateTime)ligne.dt_cmd;
                        cmdRetour.dateLivrCmd = (DateTime)ligne.dt_liv_cmd;
                    }

                    return cmdRetour;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    return cmdRetour;
                }
            }
        }

    }
}

